package cn.laixueit.controller;

import cn.laixueit.pojo.User;
import cn.laixueit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public ResponseEntity<List<User>> list(){
        List<User> userList = userService.list();
        return ResponseEntity.ok(userList);
    }

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> check(@PathVariable("data")String data,
                                         @PathVariable("type")Integer type){
        Boolean status = userService.check(data,type);
        if(status == null){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(status);
    }

    @PostMapping("/send")
    public ResponseEntity<Void> code(@RequestParam(required = true) String phone){
        userService.code(phone);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PostMapping("/register")
    public ResponseEntity<Void> register(@Validated User user, String code){
        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/query")
    public ResponseEntity<User> query(@RequestParam("username")String username,
                                      @RequestParam("password")String password){
        User user = userService.query(username,password);
        return ResponseEntity.ok(user);
    }

}
